package es.upm.dit.gsi.webIV0;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class Messenger extends Service {
	
	private final IBinder mBinder = new LocalBinder();
	
	@Override
	public IBinder onBind(Intent intent) {
		

		return mBinder;
	}
	
	public class LocalBinder extends Binder {
	      Messenger getService() {
	          return Messenger.this;
	      }
	 }
	@Override
	public void onCreate(){


	}
	public ArrayList<Plan> retrievePlans(String message) {

    	ArrayList<Plan> listPlanes=new ArrayList<Plan>();
    	try {
    		//leer URL
    		String s= message.replaceAll(" ", "%20");

    		URL url = new URL("http://minsky.gsi.dit.upm.es:5000/plan/"+s);
    		URLConnection urlConnection = url.openConnection();
    		BufferedReader in = new BufferedReader(
                                	new InputStreamReader(
                                			urlConnection.getInputStream()));
    		
    		//JSON a String
    		String line;
    		StringBuilder builder = new StringBuilder();
    		while ((line = in.readLine()) != null) {
    			builder.append(line);
    		}
    		String jString = builder.toString();

    		//String a JSONObject
    		JSONObject jObject;
    		jObject = new JSONObject(jString);  

    		
    		JSONObject jObject2 = jObject.getJSONObject("domains"); 
    		JSONArray conversacionArray = jObject2.getJSONArray("travel"); //Array de travels

    		if (conversacionArray == null)
    			throw new Exception("No plan object in json response");
    		
    		for (int i = 0; i < conversacionArray.length(); i++) {
    			JSONObject planObject = conversacionArray.getJSONObject(i);
    			
    			Plan plan=new Plan();
    			
    			JSONObject datesObject = planObject.getJSONObject("dates"); 
    			String departEnd = datesObject.getString("depart-end");
    			String returnEnd = datesObject.getString("return-end");
    			String departStart = datesObject.getString("depart-start");
    			String returnStart = datesObject.getString("return-start");
    			Plan.Dates dates=plan.new Dates(departEnd,returnEnd,departStart,returnStart);
    			
    			JSONObject locationsObject=planObject.getJSONObject("locations");
    			String from= locationsObject.getString("from");
    			String to = locationsObject.getString("to");
    			Plan.Locations locations = plan.new Locations(from,to);
    			
    			JSONObject pricesObject = planObject.getJSONObject("prices");
    			String currency = pricesObject.getString("currency");
    			int value = pricesObject.getInt("value");
    			Plan.Prices prices = plan.new Prices(currency, value);
    			
    			Plan mPlan = new Plan(dates,locations,prices); 
    			
    			listPlanes.add(mPlan);
    		}
    	} catch (Exception e) {
    		Log.v("Messenger", "Cannot parse json info: " + e.getMessage()); 		
			Toast.makeText(getBaseContext(),"Problems with url",Toast.LENGTH_LONG).show();

    	}
    	
    	return listPlanes;
    }
	
	public ArrayList<Task> retrieveTasks(String message) {

    	ArrayList<Task> listTasks=new ArrayList<Task>();
    	try {
    		//leer URL
    		String s= message.replaceAll(" ", "%20");
    			//String s=URLEncoder.encode(message, "UTF-8"); 
    		URL url = new URL("http://minsky.gsi.dit.upm.es:5000/tasks/"+s);
    		URLConnection urlConnection = url.openConnection();
    		BufferedReader in = new BufferedReader(
                                	new InputStreamReader(
                                			urlConnection.getInputStream()));
    		
    		
    		//JSON a String
    		String line;
    		StringBuilder builder = new StringBuilder();
    		while ((line = in.readLine()) != null) {
    			builder.append(line);
        		Log.v("Messenger",line);

    		}
    		
    		String jString = builder.toString();
    		if(jString==null){
    			
    			Log.v("Messenger","no in");
    		}else{
    			
        			
        			Log.v("Messenger","fuck");
        			Log.v("Messenger",jString);	
        		
    		}
    		

    		//String a JSONObject
    		JSONObject jObject;
    		Log.v("Messenger","I'm here!");
    		jObject = new JSONObject(jString);  
    		Log.v("Messenger","I'm not here!");
    		
    		
    		JSONArray conversacionArray = jObject.getJSONArray("tasks"); //Array de travels

    		if (conversacionArray == null)
    			throw new Exception("No task object in json response");
    		
    		for (int i = 0; i < conversacionArray.length(); i++) {
    			JSONObject taskObject = conversacionArray.getJSONObject(i);
    			
    			int value = taskObject.getInt("points");
    			String name = taskObject.getString("name");

    			Task mTask = new Task(value,name); 
    			
    			listTasks.add(mTask);
    		}
    	} catch (Exception e) {
    		Log.v("Messenger", "Cannot parse json info: " + e.getMessage()); 		
			Toast.makeText(getBaseContext(),"Problems with url",Toast.LENGTH_LONG).show();

    	}
    	
    	return listTasks;
    }

}
